//
//  PasswordPhotoViewController.m
//  Diary
//
//  Created by lanou3g on 16/1/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "PasswordPhotoViewController.h"
#import "PieChartView.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "PhotoViewController.h"
static inline UIColor *GetRandomUIColor()
{
    CGFloat r = arc4random() % 255;
    CGFloat g = arc4random() % 255;
    CGFloat b = arc4random() % 255;
    UIColor * color = [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1.0f];
    return color;
}

static const CGFloat dia = 250.0f;
static const NSInteger tHoleLabelTag = 7;
static const NSInteger tValueLabelTag = 77;

@interface PasswordPhotoViewController ()
<
PieChartViewDelegate,
PieChartViewDataSource
>
{
    PieChartView *pieChartView;
    UISlider *holeSlider;
    UISlider *slicesSlider;
    UILabel *holeLabel;
    UILabel *valueLabel;
    UIButton *pasButton;
}
@end

@implementation PasswordPhotoViewController



//进入相册
- (void)backBUttonAction{
    
    
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //上面密码数
    NSString *photoPasTx1 = [userDefaultes objectForKey:@"photoPasTx1"];
    //下面密码数
    NSString *photoPasTx2 = [userDefaultes objectForKey:@"photoPasTx2"];
    //密码状态
    NSInteger myPhotoInteger = [userDefaultes integerForKey:@"myPhotoInteger"];
    
    if (myPhotoInteger == 1000) {
        
        if ([valueLabel.text isEqualToString:photoPasTx2] && [holeLabel.text isEqualToString:photoPasTx1]) {
            
            PhotoViewController *photoVC = [[PhotoViewController alloc] init];
            [self.navigationController pushViewController:photoVC animated:YES];
            
            
        }
        else{
            
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"密码不对请重试!!!" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *playAlert1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertVC addAction:playAlert1];
            [self presentViewController:alertVC animated:YES completion:nil];
        }
        
    }
    else{
        
        PhotoViewController *photoVC = [[PhotoViewController alloc] init];
        [self.navigationController pushViewController:photoVC animated:YES];
    }
    
    
    
    
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (ScreenFrame_Height == 480) {
        
        pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, 80, 240, 240)];
        holeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, ScreenFrame_Height-145, 30, 30)];
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, ScreenFrame_Height-100, 30, 30)];
        holeSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-145, 240, 30)];
        slicesSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-100, 240, 30)];
        pasButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-100, 180, 200, 30)];
    }
    
    if (ScreenFrame_Height == 568) {
        
        pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, 80, 240, 240)];
        holeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, ScreenFrame_Height-165, 30, 30)];
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, ScreenFrame_Height-110, 30, 30)];
        holeSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-165, 240, 30)];
        slicesSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-110, 240, 30)];
        pasButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-100, 180, 200, 30)];
    }
    
    if (ScreenFrame_Widdth == 375) {
        
        pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-130, 100, 260, 260)];
        holeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, ScreenFrame_Height-215, 50, 50)];
        holeLabel.font = [UIFont systemFontOfSize:25.0f];
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, ScreenFrame_Height-140, 50, 50)];
        valueLabel.font = [UIFont systemFontOfSize:25.0f];
        holeSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-205, 240, 30)];
        slicesSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-130, 240, 30)];
        pasButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-100, 220, 200, 30)];
    }
    
    if (ScreenFrame_Widdth == 414) {
        
        pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-140, 120, 280, 280)];
        holeLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, ScreenFrame_Height-255, 50, 50)];
        holeLabel.font = [UIFont systemFontOfSize:25.0f];
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, ScreenFrame_Height-160, 50, 50)];
        valueLabel.font = [UIFont systemFontOfSize:25.0f];
        holeSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-245, 240, 30)];
        slicesSlider = [[UISlider alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-120, ScreenFrame_Height-150, 240, 30)];
        pasButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenFrame_Widdth/2-100, 240, 200, 30)];
    }
    
    
    
    pieChartView.delegate = self;
    pieChartView.datasource = self;
    [self.view addSubview:pieChartView];
    
    
   
    [self.view addSubview:holeLabel];
    
    
    [self.view addSubview:valueLabel];
    
   
    holeSlider.tag = tHoleLabelTag;
    [holeSlider addTarget:self action:@selector(didChangeValueForSlider:)
         forControlEvents:UIControlEventValueChanged];
    holeSlider.minimumValue = 0.0f;
    holeSlider.maximumValue = dia/2 - 1;
    int max = holeSlider.maximumValue;
    holeSlider.value = arc4random() % max;
    [self.view addSubview:holeSlider];
    
   

    slicesSlider.tag = tValueLabelTag;
    [slicesSlider addTarget:self action:@selector(didChangeValueForSlider:)
           forControlEvents:UIControlEventValueChanged];
    slicesSlider.minimumValue = 0.0f;
    slicesSlider.maximumValue = 100.0f;
    slicesSlider.value = arc4random() % 100;
    [self.view addSubview:slicesSlider];
    
    
   
    [pasButton addTarget:self action:@selector(backBUttonAction) forControlEvents:UIControlEventTouchUpInside];
    // [pasButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [pasButton setTitle:@"相册" forState:UIControlStateNormal];
    pasButton.font = [UIFont systemFontOfSize:30.0f];
    [self.view addSubview:pasButton];
    
    
    [self didChangeValueForSlider:holeSlider];
    [self didChangeValueForSlider:slicesSlider];
    [pieChartView reloadData];
    
    
    
}




-(void)didChangeValueForSlider:(UISlider*)slider
{
   
        
    if (slider.tag == tValueLabelTag) valueLabel.text = [NSString stringWithFormat:@"%.0f",slider.value];
    
    if (slider.tag == tHoleLabelTag) holeLabel.text = [NSString stringWithFormat:@"%.0f",slider.value];
    [pieChartView reloadData];
     
    
    
}

#pragma mark -    PieChartViewDelegate
-(CGFloat)centerCircleRadius
{
    return holeSlider.value;
}
#pragma mark - PieChartViewDataSource
-(int)numberOfSlicesInPieChartView:(PieChartView *)pieChartView
{
    return slicesSlider.value;
}
-(UIColor *)pieChartView:(PieChartView *)pieChartView colorForSliceAtIndex:(NSUInteger)index
{
    return GetRandomUIColor();
}
-(double)pieChartView:(PieChartView *)pieChartView valueForSliceAtIndex:(NSUInteger)index
{
    return 100/slicesSlider.value;
}


@end
